package com.shujia.sql

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment

object Demo6ChangelogJson {
  def main(args: Array[String]): Unit = {
    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //使用blink的计划器
      .inStreamingMode() //使用流模型
      .build()

    //窗口table 环境
    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)


    /**
      * 读取changelog-json 格式的数据，得到的表会自动解析成一个更新的表
      *
      */

    bsTableEnv.executeSql(
      """
        |
        |CREATE TABLE avg_age (
        | clazz STRING,
        | avgAge DOUBLE
        |) WITH (
        | 'connector' = 'kafka',
        | 'topic' = 'avg_age',
        | 'properties.bootstrap.servers' = 'master:9092',
        | 'format' = 'changelog-json',
        | 'scan.startup.mode' = 'earliest-offset',
        | 'properties.group.id' = 'asdasdasd'
        |)
        |
      """.stripMargin)

    /**
      * 创建print b表，引用原表的字段
      *
      */
    bsTableEnv.executeSql(
      """
        |CREATE TABLE print_table WITH ('connector' = 'print')
        |LIKE avg_age (EXCLUDING ALL)
      """.stripMargin)


    bsTableEnv.executeSql(
      """
        |insert into print_table
        |select * from avg_age
        |
      """.stripMargin)



  }

}
